
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.condition.typereport;

import java.io.File;
import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.GetIdName;
import utility.Input;
import utility.MyDate;

public class Schedule implements Typereport 
{
	Hashtable<Object, Object> ConditionMap =new Hashtable<Object, Object>();
	String key;
	String Value;
	GetIdName gid;
	String Field="field";
	String ALIES="alies";
	String PATH ="path";
	String PERIOD="period";
	String TREESTORAGE="treestorage";
	String NODE="node";
	String LEAF="leaf";
	String schedule="schedule";
	String currentdate="currentdate";
	String startdate="startdate";
	Vector<Object> Condition = new Vector<Object>();
	Hashtable<Object, Object>Reportmap = new Hashtable<Object, Object>();
	MyDate mydate = new MyDate();
	String CONDITIONFIELD="Conditionfield";
	String CONDITIONOPERATOR="CONDITIONOPERATOR";
	String CONDITIONVALUE="Conditionvalue";
	String LOGICALOPEARATOR="Logicaloperator";
	String AND="and";
	PoolLogger pl;
	
	public Schedule() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public Vector< Object> getConditionMap() 
	{	
		return Condition;
	}

	public void getResult() 
	{	
	/*	Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		Hashtable<Object, Object> temptable = new Hashtable<Object, Object>();
		getPath(""); 
		ConditionMap.put(CONDITIONFIELD, key);
		ConditionMap.put(CONDITIONOPERATOR, ">=");
		ConditionMap.put(CONDITIONOPERATOR, startdate.trim());
		ConditionMap.put(CONDITIONVALUE, "AND");
		ConditionMap=ConvertToLower.convertHashKey(ConditionMap);	
		table.putAll(ConditionMap);
		Condition.add(table);
		PeriodDate period =new PeriodDate(database);
		ConditionMap.clear();		
		ConditionMap.put(CONDITIONFIELD, key);
		ConditionMap.put(CONDITIONOPERATOR, "<=");		
		ConditionMap.put(CONDITIONVALUE, currentdate.trim());
		ConditionMap.put(LOGICALOPEARATOR, "AND");
		ConditionMap=ConvertToLower.convertHashKey(ConditionMap);
		temptable.putAll(ConditionMap);
		Condition.add(temptable);*/
		
		
	}	
	

	public String getPath(String ReportName) 
	{	
		RecordSearch sr =new RecordSearch();
		String scheduleobject= gid.getId(Reportmap.get(schedule).toString());
		Hashtable<Object,Object> temptable =new Hashtable<Object, Object>();
		temptable.put("pid",PERIOD);
		temptable.put("mid", scheduleobject);
		temptable.put("td", "null");
		sr.setConditionMap(temptable);
		String line[] = sr.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			PERIOD = splitline[Column.pv_index-1];
			if(PERIOD.equals("0"))
				PERIOD = splitline[Column.vt_index-1];
		}
		temptable.put("pid",currentdate);
		sr.setConditionMap(temptable);
		line =  sr.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			currentdate = splitline[Column.pv_index-1];
			if(currentdate.equals("0"))
				currentdate = splitline[Column.vt_index-1];
		/*	else
				currentdate = gid.getItem(currentdate);*/
		}
		temptable.put("pid",startdate);
		sr.setConditionMap(temptable);
		line =  sr.getArrayFromResultSet();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			startdate = splitline[Column.pv_index-1];
			if(startdate.equals("0"))
				startdate = splitline[Column.vt_index-1];
		/*	else
				startdate = gid.getItem(startdate);*/
		}
		
		temptable.clear();
		temptable.put("mid", PERIOD);
		temptable.put("pid",TREESTORAGE );
		sr.setConditionMap(temptable);
		line = sr.getArrayFromResultSet();
		String Treestorage="";
		for(int i=0;i<line.length;i++)
		{
			String split[] = line[i].split("\t");
			Treestorage = split[Column.pv_index-1];
		}
		
		temptable.clear();
		temptable.put("mid", Treestorage);
		temptable.put("pid", NODE);
		
		sr.setConditionMap(temptable);
		line =  sr.getArrayFromResultSet();
		temptable.clear();
		for(int i=0;i<line.length;i++)
		{
			String split[] = line[i].split("\t");
			String value =  split[Column.pv_index-1];
			if(value.equals("0"))
				value = split[Column.vt_index-1];
			else
				value = gid.getItem(value);
			if(value.equalsIgnoreCase("year"))
				value =  Integer.toString(mydate.getYear());
			else	if(value.equalsIgnoreCase("month"))
				value = Integer.toString(mydate.getMonth());	
			temptable.put(gid.getItem(split[Column.ref_index-1]),value);
		}
		String path ="";
		for(int i=1;i<=temptable.size();i++)
		{
			path =  path+"//"+temptable.get(Integer.toString(i));
		}
		path = path +"//";
		String storageobject= gid.getId(Reportmap.get("storage").toString());
		temptable.clear();
		temptable.put("pid", PATH);
		temptable.put("mid", storageobject);
		sr.setConditionMap(temptable);
		line =  sr.getArrayFromResultSet();
		
		String storagepath="";
		for(int i=0;i<line.length;i++)
		{
			String splitline[] =line[i].split("\t");
			storagepath = splitline[Column.pv_index-1];
			if(storagepath.equals("0"))
				storagepath = splitline[Column.vt_index-1];
			else
				storagepath = gid.getItem(storagepath);
		}
		path = Input.ROOT +  storagepath+ "SchedulReport//" + ReportName.toLowerCase()  + "//" +  path    ;
		File file = new File(path);
		file.mkdirs();
		path =  path + startdate.trim() + Input.FILRFORMAT;
		return path;
	}

	public void setData(String key, String Value, Hashtable<Object, Object>Reportmap) 
	{
		this.key = "order_date";		
		this.Value =Value;
		gid = new GetIdName();
		key = gid.getId(key);
		PATH = gid.getId(PATH); 
		PERIOD = gid.getId(PERIOD);
		TREESTORAGE = gid.getId(TREESTORAGE);
		NODE = gid.getId(NODE);
		LEAF = gid.getId(LEAF);
		currentdate  = gid.getId(currentdate);
		startdate =gid.getId(startdate);
		CONDITIONOPERATOR =gid.getId(CONDITIONOPERATOR);
		LOGICALOPEARATOR = gid.getId(LOGICALOPEARATOR);
		CONDITIONFIELD = gid.getId(CONDITIONFIELD);		
		this.Reportmap.putAll(Reportmap);  
	}	
	
	public Vector<Object> getFieldVector() 
	{
		Hashtable<Object, Object> fieldhash = new Hashtable<Object, Object>();
		fieldhash.put(Field, key);
		fieldhash.put(ALIES, "schedule_date");
		Vector<Object>Fieldvector = new Vector<Object>();
		Fieldvector.add(fieldhash);
		return Fieldvector;
	}

}
